package com.cheer.ba.presenter.impl;

import android.content.Context;

import com.cheer.ba.cache.preference.EntityCache;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.ConstantModel;
import com.cheer.ba.model.PointModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.model.UserInfoModel;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.view.base.IViewController;
import com.tencent.tinker.android.dex.Code;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 过度界面数据处理
 */
public class TransitPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;
    private EntityCache<UserInfoModel> mUserInfoCache;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
        mUserInfoCache = new EntityCache<UserInfoModel>(mContext, UserInfoModel.class, "UserInfo");
    }

    /**
     * 获得系统常量
     */
    public void getSystemConstant(String key, final String tag) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        Call<ResponseData<ConstantModel>> call = mUserService.getConstant(9002, key);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<ConstantModel>>() {
            @Override
            public void onResponse(Call<ResponseData<ConstantModel>> call, Response<ResponseData<ConstantModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<ConstantModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        ConstantModel model = res.getData();
                        if (null != model) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(model, tag);
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<ConstantModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    /**
     * 查询用户信息
     */
    public void getUserInfo() {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET);
            return;
        }
        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING);
        Call<ResponseData<UserInfoModel>> call = mUserService.checkUserInfo(1003, Session.getUserId());
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<UserInfoModel>>() {
            @Override
            public void onResponse(Call<ResponseData<UserInfoModel>> call, Response<ResponseData<UserInfoModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<UserInfoModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        UserInfoModel userModel = res.getData();
                        if (null != userModel) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL);
                            mView.updateViewWithTag(userModel, ConstantTag.MY_AUTH_QYE);
                            try {
                                mUserInfoCache.putEntity(userModel);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return;
                        }
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }

            @Override
            public void onFailure(Call<ResponseData<UserInfoModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA);
            }
        });
    }

    @Override
    public void detachView() {
    }

    @Override
    public UserInfoModel getCacheData() {
        return mUserInfoCache.getEntity(UserInfoModel.class);
    }
}
